package com.mego.msnavi.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Poi;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.mego.msnavi.R;
import com.mego.msnavi.util.SystemCenter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements Inputtips.InputtipsListener, PoiSearch.OnPoiSearchListener {

    private EditText edit_destname;
    private Button bt_navi;
    private ProgressBar progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edit_destname = (EditText) findViewById(R.id.edit_destname);
        bt_navi = (Button) findViewById(R.id.bt_navi);
        progress = (ProgressBar) this.findViewById(R.id.progress);
    }

    private long lastTime = 0;

    public void onNavi(View view) {
        long secondTime = System.currentTimeMillis();
        long X = secondTime - lastTime;
        if (X < 5000) {
            return;
        }
        lastTime = secondTime;
        String name = edit_destname.getText().toString();

        progress.setVisibility(View.VISIBLE);

        InputtipsQuery inputquery = new InputtipsQuery(name, city);
        Inputtips inputTips = new Inputtips(getApplicationContext(), inputquery);
        inputTips.setInputtipsListener(this);
        inputTips.requestInputtipsAsyn();


//        Intent intent = new Intent(this, WalkRouteCalculateActivity.class);
//        startActivity(intent);
    }


    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {

    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int errorCode) {
        try {
            //继续查询目的地的入口（不需要出口）
            LatLng latLng = null;
            if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
                if (poiItem == null) {
                    return;
                }
                LatLonPoint enterP = poiItem.getEnter();
                if (enterP != null) {
                    latLng = new LatLng(enterP.getLatitude(), enterP.getLongitude());
                }
            }

            Poi poi;
            if (latLng != null) {
                poi = new Poi(selectedPoi.getName(), latLng, selectedPoi.getPoiId());
            } else {
                poi = selectedPoi;
            }
            progress.setVisibility(View.GONE);
            SystemCenter.getInstance().poi = poi;
            Intent intent = new Intent(this, WalkRouteCalculateActivity.class);
            startActivity(intent);

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private List<Tip> mCurrentTipList;
    private Poi selectedPoi;
    String city = "上海市";

    @Override
    public void onGetInputtips(List<Tip> tipList, int rCode) {
        try {
            if (rCode == 1000) {
                mCurrentTipList = new ArrayList<Tip>();
                Tip selectTip = null;//默认使用第一个tip
                for (Tip tip : tipList) {
                    if (null == tip.getPoint()) {
                        continue;
                    }
                    if (selectTip == null) {
                        selectTip = tip;
                    }
                    mCurrentTipList.add(tip);
                }

                if (null == mCurrentTipList || mCurrentTipList.isEmpty()) {
                    Toast.makeText(this, "抱歉，没有搜索到结果，请换个关键词试试!", Toast.LENGTH_SHORT);
                } else {
                    //查询第一个tip selectTip
                    selectedPoi = new Poi(selectTip.getName(), new LatLng(selectTip.getPoint().getLatitude(), selectTip.getPoint().getLongitude()), selectTip.getPoiID());
                    if (!TextUtils.isEmpty(selectedPoi.getPoiId())) {
                        PoiSearch.Query query = new PoiSearch.Query(selectedPoi.getName(), "", city);
                        query.setDistanceSort(false);
                        query.requireSubPois(true);
                        PoiSearch poiSearch = new PoiSearch(getApplicationContext(), query);
                        poiSearch.setOnPoiSearchListener(this);
                        poiSearch.searchPOIIdAsyn(selectedPoi.getPoiId());
                    }
                }
            } else {
                Toast.makeText(this, "出错了，请稍后重试!", Toast.LENGTH_SHORT);
            }
        } catch (Throwable e) {
            Toast.makeText(this, "出错了，请稍后重试!", Toast.LENGTH_SHORT);

        }
    }
}

